﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

/***********************************
 *             Padawer             *
 ***********************************/

namespace LumenLiquid._2D
{
    public class TextureManager
    {
        #region Fields
        //[CShark] Changed Hastable to dictionary, because dictionary is 20 milliseconds faster. Thats realy much! (Thinking of 200 textures for the landscapes...)
        /// <summary>
        /// Dictionary to load all Texture2D.
        /// </summary>
        private Dictionary<string, Texture2D> table;
        #endregion

        #region Methods
        /// <summary>
        /// Constructor. TextureManager creates an instance of Hashtable.
        /// </summary>
        public TextureManager()
        {
            table = new Dictionary<string, Texture2D>();
        }

        /// <summary>
        ///  Add adds a new Texture2D in the Hashtable indexed by a String.
        /// </summary>
        /// <param name="ImageName">
        /// String to index the Texture2D in the Hashtable is passed in ImageName.
        /// </param>
        /// <param name="ImageValue">
        /// Texture2D to add in the Hashtable is passed in ImageValue.
        /// </param>
        public void Add(String ImageName, Texture2D ImageValue)
        {
            table.Add(ImageName, ImageValue);
        }

        /// <summary>
        ///  Add adds a new Texture2D in the Hashtable indexed by a String.
        /// </summary>
        /// <param name="ImageName">
        /// String to index the Texture2D in the Hashtable is passed in ImageName.
        /// </param>
        /// <param name="ImageValue">
        /// Texture2D to add in the Hashtable is passed in ImageValue.
        /// </param>
        /// <param name="Overwrite">
        /// Whether the Add function should Overwrite existing images
        /// </param>
        public void Add(String ImageName, Texture2D ImageValue, bool Overwrite)
        {
            if (table.ContainsKey(ImageName) && Overwrite)
            {
                table.Remove(ImageName);
            }

            Add(ImageName, ImageValue);
        }

        /// <summary>
        /// Overwrite of Add method loads the Texture2D and inserts it in the Hashtable.
        /// If it is not possible to load the Textrue2D it write a error message in the console.
        /// <param name = "ImageName">
        /// String to index the Texture2D in the Hashtable is passed in ImageName.
        /// </param>
        public void Add(String ImageName)
        {
            Texture2D AuxValue;
            try
            {
                AuxValue = Global.Content.Load<Texture2D>(Global.content_Imagepath + "\\" + ImageName);
                table.Add(ImageName, AuxValue);
            }
            catch
            {
                Global.DebugLog.Log("Error: can't find the image: " + ImageName);
            }
        }

        /// <summary>
        /// GetTexture returns the Texture2D of its argument.
        /// <param name = "ImageName">
        /// String to search the Texture2D is passed in ImageName.
        /// </param>
        public Texture2D GetTexture(String ImageName)
        {
            return (Texture2D)table[ImageName];
        }

        /// <summary>
        /// GetSize returns the Size of the desired Texture
        /// </summary>
        /// <param name="ImageName">The name of the searched Texture</param>
        /// <returns></returns>
        public Vector2 GetSize(String ImageName)
        {
            return new Vector2(table[ImageName].Width, table[ImageName].Height);
        }

        /// <summary>
        /// Delete deletes the Texture2D of its argument.
        /// <param name = "ImageName">
        /// String to search and delete the Texture2D is passed in ImageName.
        /// </param>
        public void Delete(String name)
        {
            table.Remove(name);
        }

        public Texture2D this[string ImageName]
        {
            get { return GetTexture(ImageName); }
        }
        #endregion
    }
}
